package com.offcn.controller;

import com.offcn.app.test.CreateMainPicture;
import com.offcn.pojo.Member;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

@Controller

public class PagesController {


     /*
       需求：跳转到主页面
       URL:http://localhost:80/eastCMS/pages/main
       逻辑：
           1：接收请求
           2：跳转到main.jsp
     */

    @RequestMapping("/pages/{pageName}")
    public String pageTurn(@PathVariable String pageName) {


        return pageName;
    }


    /*
        需求：展示二维码
        URL：http://localhost:80/eastCMS/toLookImage/
        逻辑：
            1；使用工具类 将数据写到二维码本地图片上
            2：将图片画到 浏览器
        坑：请求最后面有一个/的时候
        */
    @RequestMapping("toLookImage/")

    public void toLookImage(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Member sessionMember = (Member) request.getSession().getAttribute("member");

        File file = new File("D:\\picture");

        CreateMainPicture.showCreateMainPicture(sessionMember.getMname(),sessionMember.getMaddress(),file);

        OutputStream outputStream = response.getOutputStream();
        FileInputStream fis = new FileInputStream(file);
        int len = -1;
        byte[] b = new byte[1024];

        while ((len = fis.read(b)) != -1) {

            outputStream.write(b, 0, len);

            outputStream.flush();
        }
        outputStream.close();
        fis.close();
    }

//       localhost/eastCMS/pages/employee-update/1000?_=1609118983948

}
